package com.koron.order.linedefect.defectPending.plugin;

import com.alibaba.fastjson.JSON;
import com.koron.bean.base.Response;
import com.koron.bean.system.post.query.PostUserQuery;
import com.koron.bean.system.post.vo.PostUserVo;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.business.system.feign.SystemFeignService;
import com.koron.order.linedefect.defectPending.bean.po.DefectPendingPo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName DefectPendingPlugin
 * @description
 * @Author zhouj
 * @Date 2022/2/9 11:44
 */
@Slf4j
@RequestMapping("/lineDefectPending/plugin/waitReview/")
@RestController("lineDefectPendingPlugin")
@Api(tags = "缺陷待处理-插件")
public class DefectPendingPlugin {

    @Autowired
    private SystemFeignService systemFeignService;

    //设备管理单位领导审核 节点配置出口规则
    @PostMapping("/task1/submitter")
    public Response<List<WorkflowUser>> submitter1(@RequestBody SubmitterParam submitterParam) {

        String formVariables = submitterParam.getFormVariables();
        List<WorkflowUser> workflowUsers = submitterParam.getUsers();

        //返回对象
        List<WorkflowUser> result = new ArrayList<>();

        DefectPendingPo defectPendingPo = getBussinessBean(submitterParam);
        /*
                节点1的过滤处理  站室领导审核
                1、处理人	站长、副站长、安全主任（岗位）
                2、操作类型	驳回、沟通、转办
                3、自动过滤规则	只发送给创建人所在单位的站长、副站长、安全主任
                4、必须变更审批人的节点	无（默认）
                5、通知方式	待办
                */
        //拿到创建人的单位
        String deptId = defectPendingPo.getDeptId();

        List<String> orgIdList = new ArrayList<>();
        orgIdList.add(deptId);


        /*name code type*/
        //先分类
        List<WorkflowUser> gwList = workflowUsers.stream().filter(p -> 2 == p.getType()).collect(Collectors.toList());

        Assert.notEmpty(gwList, "配置的岗位不能为空");

        //分别通过各自id/code进行查询，或者过滤，最终转化为人员
        List<String> gwIdList = gwList.stream().map(WorkflowUser::getCode).collect(Collectors.toList());

        //先将岗位转化为对应的人员列表，利用人员的单位==缺陷工单的设备管理单位
        PostUserQuery postUserQuery = new PostUserQuery();
        postUserQuery.setPostIdList(gwIdList);
        postUserQuery.setOrgIdList(orgIdList);
        //拿到岗位下的人
        List<PostUserVo> postUserVos = getPostList(postUserQuery);

        //转换为返回对象
        convertUser(postUserVos, result);

        return Response.ok(result);
    }

    private DefectPendingPo getBussinessBean(SubmitterParam submitterParam) {
        return JSON.parseObject(submitterParam.getFormVariables(), DefectPendingPo.class);
    }

    private List<PostUserVo> getPostList(PostUserQuery postUserQuery) {
        Response<List<PostUserVo>> listResponse = systemFeignService.queryPostList(postUserQuery);
        List<PostUserVo> postUserVos = listResponse.getData();
        return postUserVos;
    }

    private void convertUser(List<PostUserVo> postUserVos, List<WorkflowUser> result) {
        //转换为返回对象
        if (CollectionUtils.isEmpty(postUserVos)) {
            return;
        }
        for (PostUserVo postUserVo : postUserVos) {
            WorkflowUser workflowUser = new WorkflowUser();
            workflowUser.setCode(postUserVo.getUserAccount());
            workflowUser.setName(postUserVo.getUserName());
            result.add(workflowUser);
        }
    }

}
